/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package evidence.model.modeloveTridy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Protheo
 */
public class Klienti implements Serializable, Runnable {

    public static final long serialVersionUID = 1;
    private Map<String, Klient> klienti;
    private File soubor;

    public Klienti(String klientiCesta) {
        ObjectInputStream is = null;
        soubor = new File(klientiCesta);
        if (!soubor.exists()) {
            soubor = new File("userdata" + File.separator + "klienti.sou");
            klienti = new HashMap<String, Klient>();
            try {
                soubor.createNewFile();
            } catch (IOException ex) {
                Logger.getLogger(Klienti.class.getName()).log(Level.SEVERE, null, ex);
            }
            save();
        }
        try {
            is = new ObjectInputStream(new FileInputStream(soubor));
            klienti = (Map<String, Klient>) is.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Klienti.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Klienti.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(Klienti.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public final void save() {
        ObjectOutputStream os = null;
        try {
            os = new ObjectOutputStream(new FileOutputStream(soubor));
            os.writeObject(klienti);
            os.flush();
            os.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Klienti.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Klienti.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                os.close();
            } catch (IOException ex) {
                Logger.getLogger(Klienti.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void addKlient(String jmeno) {
        if (!this.isKlient(jmeno)) {
            klienti.put(jmeno, new Klient(klienti.size(), jmeno));
        }
    }

    public Boolean isKlient(String jmeno) {
        return klienti.containsKey(jmeno);
    }

    public List<String> getKlienti() {
        List<String> kklienti = new ArrayList<String>(this.klienti.keySet());
        Collections.sort(kklienti);
        return kklienti;
    }

    public Klient getKlient(String klient) {
        return this.klienti.get(klient);
    }

    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String toString() {
        return "Klienti{" + "klienti=" + klienti + '}';
    }
}
